package com.runshu.newsmartark.mvp.presenter;

import android.os.AsyncTask;

import com.google.gson.Gson;
import com.runshu.newsmartark.base.MyApplication;
import com.runshu.newsmartark.common.ConstantUrl;
import com.runshu.newsmartark.dbutil.DBUtils;
import com.runshu.newsmartark.dbutil.ProSelfEntity;
import com.runshu.newsmartark.entity.NewTakeStockEntity;
import com.runshu.newsmartark.entity.ProDetailEntity;
import com.runshu.newsmartark.entity.ProductsBean;
import com.runshu.newsmartark.mvp.contract.ProDetailContract;
import com.runshu.newsmartark.mvp.contract.ProductsByCategoryContract;
import com.runshu.newsmartark.network.netbean.ResponseBean;
import com.runshu.newsmartark.network.netinterface.MyCallBack;
import com.runshu.newsmartark.util.common.SharedPreferencesUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Demo class
 *
 * @author yu
 * @date 12/7/20
 * @decs：
 */

public class ProDetailPresenter implements ProDetailContract.Presenter {
    private ProDetailContract.View mview;
    private ProDetailContract.Model mModel;
    String venderName = "";
    String regularName = "";
    String productName = "";

    public ProDetailPresenter(ProDetailContract.View mview, ProDetailContract.Model mModel) {
        this.mview = mview;
        this.mModel = mModel;
    }

    @Override
    public void getProDetail(String manufacturer, String productNo, String model, String stockName, String expiryDate, int pageSize, int pageNum, int type) {
        if (type != 0) {
            ProDBTask cg = new ProDBTask( mview, this );
            Map<String, String> params = new HashMap<>();
            params.put( "productNo", productNo );
            params.put( "model", model );
            params.put( "manufacturer", manufacturer );
            params.put( "expiryDate", expiryDate );
            cg.execute( params );
        } else {
            mModel.getProDetail( manufacturer, productNo, model, stockName, expiryDate, pageSize, pageNum, type, new MyCallBack() {
                @Override
                public void onSuccess(ResponseBean data) {
                    mview.onGetProDetailSuccess( data );
                }

                @Override
                public void onError(ResponseBean data) {
                    mview.onError( data.getMessage() );
                }

                @Override
                public void onErrorForOthers(ResponseBean msg) {
                    mview.onError( msg.getMessage() );
                    super.onErrorForOthers( msg );
                }
            } );
        }
    }

    @Override
    public void getInventoryDetail(String customerId, String hospitalId, String goodsId, String hwId, int pageSize, int pageNum) {
        mModel.getInventoryDetail(customerId, hospitalId, goodsId, hwId, pageSize, pageNum, new MyCallBack() {
            @Override
            public void onSuccess(ResponseBean data) {
                mview.onGetInventoryDetailSuccess(data);
            }

            @Override
            public void onError(ResponseBean data) {
                mview.onError(data.getMessage());
            }
        });
    }

    static class ProDBTask extends AsyncTask<Map<String, String>, Integer, List<ProSelfEntity>> {
        private ProDetailContract.View view;
        private ProDetailPresenter proDetailPresenter;

        public ProDBTask(ProDetailContract.View view, ProDetailPresenter proDetailPresenter) {
            this.view = view;
            this.proDetailPresenter = proDetailPresenter;
        }

        @Override
        protected List<ProSelfEntity> doInBackground(Map<String, String>... params) {
            String productNo = params[0].get( "productNo" );
            String model = params[0].get( "model" );
            String manufacturer = params[0].get( "manufacturer" );
            String expiryDate = params[0].get( "expiryDate" );
            String vendorName = params[0].get( "vendorName" );
//            (new String[]{"全部", "近1个月", "3近个月", "近半年", "半年以上"};)
            List<ProSelfEntity> proInfoByType = DBUtils.getInstance().getProAllDao().getProInfoByType( productNo, model, manufacturer,vendorName );
            List<ProSelfEntity> result = new ArrayList<>();
            long timeTONow = 0;
            long now = System.currentTimeMillis();
            if (proInfoByType == null) return new ArrayList<>();
            if ("0".equals( expiryDate )) {
                return proInfoByType;
            } else if ("1".equals( expiryDate )) {
                timeTONow = 1000 * 3600 * 24 * 30;
            } else if ("2".equals( expiryDate )) {
                timeTONow = 1000 * 3600 * 24 * 30 * 3;
            } else if ("3".equals( expiryDate )) {
                timeTONow = 1000 * 3600 * 24 * 30 * 6;
            } else if ("4".equals( expiryDate )) {
                timeTONow = 1000 * 3600 * 24 * 30 * 3;
                for (ProSelfEntity tmp : proInfoByType) {
                    if ((tmp.getValidTimeSJC() - now) > timeTONow) {
                        result.add( tmp );
                    }
                }
                return result;
            } else {
                return proInfoByType;
            }
            for (ProSelfEntity tmp : proInfoByType) {
                if ((tmp.getValidTimeSJC() - now) < timeTONow) {
                    result.add( tmp );
                }
            }
            return result;
        }


        @Override
        protected void onPostExecute(List<ProSelfEntity> productsBeans) {
            if (productsBeans != null) {
                ProDetailEntity entity = new ProDetailEntity();
                if (productsBeans.size() > 0) {
                    ProSelfEntity proSelfEntity = productsBeans.get( 0 );
                    proDetailPresenter.venderName = proSelfEntity.getVendorName();
                    proDetailPresenter.regularName = proSelfEntity.getRegularModel();
                    proDetailPresenter.productName = proSelfEntity.getProductName();
                }
                entity.setManufacturer( proDetailPresenter.venderName );
                entity.setProductModel( proDetailPresenter.regularName );
                entity.setProductName( proDetailPresenter.productName );
                entity.setProductNum( String.valueOf( productsBeans.size() ) );
                ArrayList<ProDetailEntity.ProductDetailListBean> productDetailList = new ArrayList<>();
                for (ProSelfEntity tmp : productsBeans) {
                    ProDetailEntity.ProductDetailListBean data = new ProDetailEntity.ProductDetailListBean();
                    data.setBatchNo( tmp.getBatchNo() );
                    data.setHwName( (String) SharedPreferencesUtils.get( MyApplication.getApplication(), ConstantUrl.Share_StockName,"暂无" ));
                    data.setPeriodDate( tmp.getValidPeriodDate() );
                    data.setSerialNo( tmp.getSerialNo() );
                    data.setProType(tmp.getProType());
                    productDetailList.add( data );
                }
                entity.setProductDetailList( productDetailList );
                ResponseBean responseBean = new ResponseBean();
                responseBean.setData( new Gson().toJson( entity ) );
                responseBean.setClass( ProDetailEntity.class );
                responseBean.setCode( "200" );
                responseBean.setMessage( "成功" );
                view.onGetProDetailSuccess( responseBean );
            } else {
                view.onError( "未查询到数据" );
            }
        }
    }
}